package com.chinasofti.hrm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chinasofti.hrm.domain.User;
import com.chinasofti.hrm.service.HrmService;
import com.chinasofti.hrm.util.common.HrmConstants;
import com.chinasofti.hrm.util.tag.PageModel;

import net.sf.json.JSONObject;

/**
 * 处理用户请求控制�?
 * */
@Controller
public class UserController extends BasicController{
	
	
	
	/**
	 * 自动注入UserService
	 * */
	@Autowired
	@Qualifier("hrmService")
	private HrmService hrmService;
	
	@RequestMapping("/user/getAdmin")
	public void getAdmin(HttpServletResponse response,HttpServletRequest request){
		HttpSession session=request.getSession();
		JSONObject json=new JSONObject();
		json.put("message", "");
		
		User user=(User) session.getAttribute(HrmConstants.USER_SESSION);
		json.put("admin", user.getStatus().intValue()+"");

		this.writeJson(json.toString(), response);
	}
	@RequestMapping("/user/getUserName")
	@ResponseBody
	public User getUserName(HttpServletResponse response,HttpServletRequest request){
		HttpSession session=request.getSession();
		JSONObject json=new JSONObject();
		json.put("message", "");

		User user=(User) session.getAttribute(HrmConstants.USER_SESSION);
		return user;

	}
	@RequestMapping("/user/getUserById")
	@ResponseBody
	public User getUserById(Integer id,HttpServletResponse response,HttpServletRequest request){
		User user = hrmService.findUserById(id);
		return user;

	}
	@RequestMapping("/user/editPassword")
	public void editPassword(HttpServletResponse response,HttpServletRequest request,String oldPassword,String newPassword) {
		HttpSession session=request.getSession();
		JSONObject json=new JSONObject();
		
		User user=(User) session.getAttribute(HrmConstants.USER_SESSION);
		int a=hrmService.userNumByNameAndPassword(user.getLoginname(), this.md5(oldPassword));
		json.put("message", "");
		if(a==0){
			json.put("message", "当前密码错误");
		}else{
			hrmService.upDataPasswordByName(user.getLoginname(), this.md5(newPassword));
			session.invalidate();
		}
		
		
		this.writeJson(json.toString(), response);
	}

	
	@RequestMapping("/user/gotoEditPassword")
	public String gotoEditPassword( ) {
	    return "editPassword/editPassword";
	}
	
	
	@RequestMapping(value="/user/loginOut")
	public void loginOut(HttpServletResponse response,HttpServletRequest request){
		HttpSession session=request.getSession();
		JSONObject json=new JSONObject();
		session.invalidate();
		this.writeJson(json.toString(), response);
	}
	@RequestMapping(value="/user/ajaxlogin")
	 public void ajaxlogin(String loginname,
			 String password,HttpServletResponse response,HttpServletRequest request
			 ){
		HttpSession session=request.getSession();
		JSONObject json=new JSONObject();
		json.put("message", "");
		// 调用业务逻辑组件判断用户是否可以登录
		User user = hrmService.login(loginname, this.md5(password));
		if(user == null){
			// 设置登录失败提示信息
			json.put("message", "登录名或密码错误!请重新输入");
		}else{
			session.setAttribute(HrmConstants.USER_SESSION, user);
		}
		this.writeJson(json.toString(), response);
		
	}
	

	
	
	/**
	 * 处理登录请求
	 * @param String loginname  登录�?
	 * @param String password 密码
	 * @return 跳转的视�?
	 * */
	@RequestMapping(value="/login")
	 public ModelAndView login(@RequestParam("loginname") String loginname,
			 @RequestParam("password") String password,
			 HttpSession session,
			 ModelAndView mv){
		// 调用业务逻辑组件判断用户是否可以登录
		User user = hrmService.login(loginname, this.md5(password));
		if(user != null){
			// 将用户保存到HttpSession当中
			session.setAttribute(HrmConstants.USER_SESSION, user);
			// 客户端跳转到main页面
			mv.setViewName("redirect:/main");
		}else{
			// 设置登录失败提示信息
			mv.addObject("message", "登录名或密码错误!请重新输�?");
			// 服务器内部跳转到登录页面
			mv.setViewName("forward:/loginForm");
		}
		return mv;
		
	}
	
	/**
	 * 处理查询请求
	 * @param pageIndex 请求的是第几�?
	 * @param employee 模糊查询参数
	 * @param Model model
	 * */
	@RequestMapping(value="/user/selectUser")
	 public String selectUser(Integer pageIndex,
			 @ModelAttribute User user,
			 Model model){
		System.out.println("user = " + user);
		PageModel pageModel = new PageModel();
		if(pageIndex != null){
			pageModel.setPageIndex(pageIndex);
		}
		/** 查询用户信息     */
		List<User> users = hrmService.findUser(user, pageModel);
		model.addAttribute("users", users);
		model.addAttribute("pageModel", pageModel);
		return "user/user";
		
	}
	@RequestMapping(value="/user/getUserList")
	@ResponseBody
	public Map<String,Object> getUserList(Integer pageIndex,
							 @ModelAttribute User user,
							 Model model){
		System.out.println("user = " + user);
		PageModel pageModel = new PageModel();
		if(pageIndex != null){
			pageModel.setPageIndex(pageIndex);
		}
		Map<String,Object> map = new HashMap<>();

		/** 查询用户信息     */
		List<User> users = hrmService.findUser(user, pageModel);
		map.put("users",users);
		map.put("pageModel",pageModel);
		return map;

	}
	/**
	 * 处理删除用户请求
	 * @param String ids �?要删除的id字符�?
	 * @param ModelAndView mv
	 * */
	@RequestMapping(value="/user/removeUser")
	 public ModelAndView removeUser(String ids,ModelAndView mv){
		// 分解id字符�?
		String[] idArray = ids.split(",");
		for(String id : idArray){
			// 根据id删除员工
			hrmService.removeUserById(Integer.parseInt(id));
		}
		// 设置客户端跳转到查询请求
		mv.setViewName("redirect:/user/selectUser");
		// 返回ModelAndView
		return mv;
	}
	
	
	/**
	 * 处理修改用户请求
	 * @param String flag 标记�? 1表示跳转到修改页面，2表示执行修改操作
	 * @param User user  要修改用户的对象
	 * @param ModelAndView mv
	 * */
	@RequestMapping(value="/user/updateUser")
	 public ModelAndView updateUser(
			 String flag,
			 @ModelAttribute User user,
			 ModelAndView mv){
		String pass=user.getPassword();
		if(pass!=null && (!pass.trim().equals(""))){
			if(pass.length()<32){
				user.setPassword(this.md5(pass));
			}
			
		}
		if(flag.equals("1")){
			// 根据id查询用户
			User target = hrmService.findUserById(user.getId());
			// 设置Model数据
			mv.addObject("user", target);
			// 返回修改员工页面
			mv.setViewName("user/showUpdateUser");
		}else{
			// 执行修改操作
			hrmService.modifyUser(user);
			// 设置客户端跳转到查询请求
			mv.setViewName("redirect:/user/selectUser");
		}
		// 返回
		return mv;
	}
	
	
	/**
	 * 处理添加请求
	 * @param String flag 标记�? 1表示跳转到添加页面，2表示执行添加操作
	 * @param User user  要添加用户的对象
	 * @param ModelAndView mv
	 * */
	@RequestMapping(value="/user/addUser")
	 public ModelAndView addUser(
			 String flag,
			 @ModelAttribute User user,
			 ModelAndView mv){
		String pass=user.getPassword();
		if(pass!=null && (!pass.trim().equals(""))){
			if(pass.length()<32){
				user.setPassword(this.md5(pass));
			}
			
		}
		System.out.println("aaaa=="+user.getStatus());
		if(flag.equals("1")){
			// 设置跳转到添加页�?
			mv.setViewName("user/showAddUser");
		}else{
			// 执行添加操作
			hrmService.addUser(user);
			// 设置客户端跳转到查询请求
			mv.setViewName("user/showAddUser");
		}
		// 返回
		return mv;
	}
	
}
